/**
 * Created by lx on 2017/10/9.
 */
angular.module('module.table.group')
    .controller('GroupAddController', GroupAddController);
GroupAddController.$inject = ['$state', '$stateParams', 'TableGroupService', '$localStorage'];
function GroupAddController($state, $stateParams, TableGroupService, $localStorage) {

    var vm = this;

    if (!$localStorage[environmental + "shopid"]) {
        $localStorage.$reset();
        $state.go('login');
    }

    var shopid = $localStorage[environmental + "shopid"];
    var tableid = $stateParams.tableid;
    vm.addGroup = addGroup;
    vm.sureBtn = sureBtn; // 模态框
    vm.cancelBtn = cancelBtn; // 模态框

    function addGroup(groupname) {
        $("#groupaddbtn").attr("disabled", true);
        if (!groupname) {
            vm.messageVisible = true;
            vm.message = "请输入分组名！";
            $("#groupaddbtn").attr("disabled", false);
            return;
        }
        var groupdetiel = {
            groupname: groupname,
            shopid: shopid,
        };
        TableGroupService.addTableGroup(groupdetiel, function (sucData) {
            $("#groupaddbtn").attr("disabled", false);
            if (tableid) {
                $state.go("tabledetail", {tableid: tableid});
            } else {
                $state.go("grouplist");
            }
        }, function (errData) {
            $("#groupaddbtn").attr("disabled", false);
        })
    }

    function sureBtn() {
        vm.messageVisible = false;
        $("#groupaddbtn").attr("disabled", false);

    }

    function cancelBtn() {
        vm.messageVisible = false;
        $("#groupaddbtn").attr("disabled", false);

    }
}